<template>
    <div class="inspection-detail-layout">
        <div class="divide-line-part"></div>
        <div v-show="isShowDetail" >
            <p style="text-align: left;padding-left: 0.6rem;font-size: 1.2em">巡检报告目录</p>
            <div class="inspection-main-content-part" v-for="(item,index) in level_one">
                <p class="inspection-content-part blue-color bottom-line-part" @click="reportHandlerClick(index)">{{item.item_name}}</p>
                <!--<div v-if="item.item_uuid==='YCHZ'" class="inspection-title-part">-->
                    <!--<p>完整报告</p>-->
                <!--</div>-->
            </div>
        </div>

        <div v-show="!isShowDetail" class="inspection-wait-part">
            <div>
                <img src="../../assets/shengchengbaogao.png" class="wait-logo" alt="">
                <p class="wait-content">等待重新生成报告</p>
            </div>
        </div>

        <div class="divide-big-part"></div>

        <div class="inspection-info-part">
            <p>巡检单号：</p>
            <p class="info-text-part">{{repairNO}}</p>
        </div>

        <div class="inspection-info-part">
            <p>巡检机构：</p>
            <p class="info-text-part">{{repairInst}}</p>
        </div>

        <div class="inspection-info-part">
            <p>开始时间：</p>
            <p class="info-text-part">{{startTime}}</p>
        </div>

        <div v-if="taskState==='99'||taskState==='98' || taskState==='90'" class="inspection-info-part bottom-line-part">
            <p>结束时间：</p>
            <p class="info-text-part">{{endTime}}</p>
        </div>

        <div class="inspection-operation-part buttom-tabbar" v-if="taskState==='20'">
            <!--退回按钮-->
            <a  href="javascript:;"
                @click="retreatHandleClick"
                class="weui-btn weui-btn_primary horizontal-part">退回</a>
            <!--确认按钮-->
            <a  href="javascript:;"
                @click="handleClick"
                class="weui-btn weui-btn_primary horizontal-part">确认</a>
        </div>

    </div>
</template>

<script>
    import {getExceptionReport,getReportSummary,inspectionConfirm,inspectionReject} from "../../pcof/post";
    import {userInfoData,bdLatitude,bdLongitude,inspectionType} from '../../globalVariables';
    import util from '../../util.js'
    export default {
        name: "inspectionSiteDetail",
        data() {
            return {
                isShowDetail: true,
                level_one: [],
                repairNO: '',
                repairInst: '',
                startTime: '',
                endTime: '',
                taskState: '',//报修单/巡检单通用的状态码State
                taskUUid: '',
            }
        },
        async created() {
            //接受上个页面传值 在实例创建完成后被立即调用
            this.taskUUid = this.$route.params.taskUUid;
            this.$dialog.loading.open('加载中...');
            this.getExceptionReportFunc();
            this.getReportSummaryFunc();
        },
        methods: {
            async getExceptionReportFunc() {
                var exceptionData = await getExceptionReport(JSON.parse(localStorage.getItem(userInfoData)).mobile,this.taskUUid);
                if (exceptionData.data.data.list.length === 0 || exceptionData.data.data.list === null) {
                } else {
                    this.level_one = [{item_uuid: "YCHZ", item_name: "异常汇总", is_completed: true}];
                }
            },
            async getReportSummaryFunc() {
                var response = await getReportSummary(JSON.parse(localStorage.getItem(userInfoData)).mobile,this.taskUUid);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    var inspectionData = response.data.data;
                    this.repairNO = inspectionData.code;
                    this.repairInst = inspectionData.inst_name;
                    this.startTime = util.substringTime(inspectionData.start_time);
                    this.endTime = util.substringTime(inspectionData.end_time);

                    this.level_one = [...this.level_one,...inspectionData.level_one];
                    var summaryData = response.data.data;
                    if ((summaryData.images === "" || summaryData.images === undefined || summaryData.images === null) &&
                        (summaryData.suggestion === "" || summaryData.suggestion === undefined || summaryData.suggestion === null)) {

                    } else {
                        var advice = [{item_uuid: "YJYJY", item_name: "意见与建议", is_completed: true}];
                        this.level_one = [...this.level_one,...advice];
                    }
                    localStorage.setItem(inspectionType,JSON.stringify(this.level_one));
                    localStorage.setItem(bdLongitude,inspectionData.longitude);
                    localStorage.setItem(bdLatitude,inspectionData.latitude);
                    this.taskState = inspectionData.state;
                    if (this.taskState === '11') {
                        this.isShowDetail = false;
                    }
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            async inspectionRejectFunc() {
                var response = await inspectionReject(JSON.parse(localStorage.getItem(userInfoData)).mobile,this.taskUUid);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: '已退回',
                        timeout: 1500
                    });
                    this.getExceptionReportFunc();
                    this.getReportSummaryFunc();

                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            async inspectionConfirmFunc() {
                var response = await inspectionConfirm(JSON.parse(localStorage.getItem(userInfoData)).mobile,this.taskUUid);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: '已确认，本次巡检结束',
                        timeout: 1500
                    });
                    this.getExceptionReportFunc();
                    this.getReportSummaryFunc();

                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
             retreatHandleClick() {
                this.$dialog.confirm({
                    title: '',
                    mes: '是否回调该巡检报告，由巡检人员重新编写？',
                    opts: () => {
                        this.$dialog.loading.open('加载中...');
                        this.inspectionRejectFunc();
                    }
                });
            },
            handleClick() {
                this.$dialog.confirm({
                    title: '',
                    mes: '是否确认该巡检报告？',
                    opts: async () => {
                        this.$dialog.loading.open('加载中...');
                        this.inspectionConfirmFunc();
                    }
                });
            },
            reportHandlerClick(index) {
                this.$router.push({name: 'inspectionReportPage',params:{taskUUid: this.taskUUid,index: index}})
            }
        }

    }
</script>

<style lang="scss">
    .inspection-detail-layout {
        padding-top: 1rem;

        .inspection-title-part {
            background-color: #fafafa;
            width: 100%;
            padding: 0.04rem;
            padding-left: 0.6rem;
            padding-right: 0.6rem;
        }

        .inspection-wait-part {
            padding-top: 1.2rem;
            padding-bottom: 1rem;

            .wait-logo {
                width: 1.2rem;
                height: 1.2rem;
            }

            .wait-content {
                padding-top: 0.1rem;
                font-size: 1.1em;
            }

        }

        .inspection-main-content-part {
            display: flex;
            flex-direction: column;
            text-align: left;

            .inspection-content-part {
                font-size: 1.2em;
                padding-bottom: 0.1rem;
                padding-top: 0.1rem;
                padding-left: 0.6rem;
                padding-right: 0.6rem;
            }
        }

        .inspection-info-part {
            display: flex;
            padding-top: 0.1rem;
            padding-left: 0.6rem;
            padding-right: 0.6rem;
            font-size: 1.2em;

            .info-text-part {
                color: #a0a0a0;
            }
        }

        .inspection-operation-part {
            display: flex;
            padding-top: 0.3rem;
            padding-bottom: 0.3rem;
            background: #fafafa;

            .horizontal-part {
                margin: 0 auto;
                width: 40%;
            }

            .weui-btn {
                display: flex;
                height: 0.6rem;
                align-items: center;
                justify-content: center;
                font-size: 1.2em;
            }
        }


    }
</style>